<template>
  <div class="top">
    <div class="content">
      <!-- 左侧 -->
      <div class="left" @click="goHome">
        <img src="@/assets/images/logo.webp" alt="" />
        <p>{{$t('title.name')}}</p>
      </div>
      <div class="right">
       <el-dropdown>
          <span>
            
           <span>{{$t('language.name')}}：{{language}}</span> 
            <el-icon class="el-icon--right">
              <arrow-down />
            </el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="changeZh">{{$t('language.zh')}}</el-dropdown-item >
              <el-dropdown-item @click="changeEn">{{$t('language.en')}}</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>


        <p class="help">{{$t('admine.help')}}</p>
        <!-- 如果没有用户名字:显示登录注册 -->
        <p class="login" @click="login" v-if="!userStore.userInfo.name">{{$t('admine.loginOrlogout')}}</p>
        <!-- 如果有用户信息展示用户信息 -->
        <el-dropdown v-else>
          <span>
            {{ userStore.userInfo.name }}
            <el-icon class="el-icon--right">
              <arrow-down />
            </el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="goUser('/user/certification')"
                >{{$t('admine.certification')}}</el-dropdown-item
              >
              <el-dropdown-item @click="goUser('/user/order')">{{$t('admine.order')}}</el-dropdown-item>
              <el-dropdown-item @click="goUser('/user/patient')"
                >{{$t('admine.patientManager')}}</el-dropdown-item
              >
              <el-dropdown-item @click="logout">{{$t('admine.logOut')}}</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>

</template>

<script setup lang="ts">
import { ArrowDown} from "@element-plus/icons-vue";
//引入路由器
import { useRouter } from "vue-router";

//获取user仓库的数据( visiable)可以控制login组件的对话框显示与隐藏
import useUserStore from "@/store/modules/user";

// *****************************
import {getCurrentInstance,onMounted,ref } from 'vue'

let language=ref<string>('');
// 定义所要传给父组件的值
let value = ref<String>('')
// ?表示前面如果返回null,后面不执行
let $this=getCurrentInstance()?.appContext.config.globalProperties as any
onMounted(() => { 
  language=$this.$i18n.locale=='zh-cn'?'中文':'English',
  value=$this.$i18n.locale
  transValue()
});
// 切换中文
const  changeZh=()=>{
  // 切换中文
  $this.$i18n.locale="zh-cn",
  language='中文',
  value='zh-cn'
  transValue()
}

// 切换英文
const changeEn=()=>{
  // 切换英文
  $this.$i18n.locale="en",
  language='English',
  value='en'
  transValue()
}
// 使用defineEmits注册一个自定义事件
const emit = defineEmits(["getValue"])

// 点击事件触发emit，去调用我们注册的自定义事件getValue,并传递value参数至父组件
const transValue = () => {
  emit("getValue", value)
}

   









let userStore = useUserStore();

let $router = useRouter();
const goHome = () => {
  //编程式导航跳转到首页
  $router.push({ path: "/home" });
};
//点击登录与注册按钮的时候弹出对话框
const login = () => {
  userStore.visiable = true;
};
//退出登录按钮的回调
const logout = () => {
  //通知pinia仓库清除用户相关的信息
  userStore.logout();
  //编程式导航路由跳转到首页
  $router.push({ path: "/home" });
};

//点击顶部下拉菜单进行路由跳转
const goUser = (path: string) => {
  $router.push({ path: path });
};
</script>

<style scoped lang="scss">
.top {
  position: fixed;
  z-index: 999;
  width: 100%;
  height: 70px;

  display: flex;
  justify-content: center;
  .content {
    width: 1200px;
    height: 70px;
    background: white;
    display: flex;
    justify-content: space-between;
    .left {
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        width: 50px;
        height: 50px;
        margin-right: 10px;
      }
      p {
        font-size: 20px;
        color: #55a6fe;
      }
    }
    .right {
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 14px;
      color: #bbb;
    
      .help {
        margin-right: 10px;
      }
    }
  }
}
</style>
